//
//  ScreenViewController.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/10.
//

import UIKit
typealias BlockScreeningCondition = (_ selectModel: SelectparameterModel) ->Void
class ScreenViewController: BaseViewController {
    var screeningConditionBlock: BlockScreeningCondition!
    var tableView: BaseTableView!
    var resetBtn: UIButton!//重置
    var filtratBtn: UIButton!//筛选
    var priceStart = ""//价格区间-开始
    var priceEnd = ""//价格区间-结束
    var selectModel: SelectparameterModel!
    var sectionsArray:[String] = []
    var seatArray = NSMutableArray()//座位
    var brandNameArray = NSMutableArray() //品牌
    var seriesArray  = NSMutableArray() //车系
    var vehicleModelArray: [CarModelDataList] = []//车型
    var isHomeEnter = 0 //1:首页进来
    var originData: FilterDataModel? //海外车进来不为nil
    var seaCarFlag: Bool = false //海外车标识
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navBar.leftButton.isHidden = true
        self.navBar.wr_setBottomLineHidden(hidden: true)
        self.sectionsArray = [
            MMLocalizedString("brands", comment: "品牌"),
            MMLocalizedString("car_series", comment: "车系"),
            MMLocalizedString("price_range", comment: "价格"),
            MMLocalizedString("seat", comment: "座位"),
            self.seaCarFlag ? MMLocalizedString("year_manufacture", comment: "年份") : MMLocalizedString("year_manufacture_local", comment: "制造年份")
            ,
            MMLocalizedString("vehicle_model", comment: "车型")
        ]
        self.initNav()
        self.initTalbeView()
        self.dealData()
        self.requestCarListData()

    }
    
    func initNav(){
        
        let closeBtn = UIButton(frame: CGRect(x: 0, y: NavBarHight - 44, width: 40, height: 44))
        closeBtn.setImage(UIImage(named: "icon_close"), for: .normal)
        closeBtn.addTarget(self, action: #selector(closeBtnAntion), for: .touchUpInside)
        self.navBar.addSubview(closeBtn)
        
        
        let titleLab = UILabel(frame: CGRect(x: closeBtn.frame.maxX, y: NavBarHight - 44, width: 60, height: 44))
        titleLab.text = MMLocalizedString("filtrate", comment: "筛选")
        titleLab.textColor = Theme.titleLabColor
        titleLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        titleLab.textAlignment = .left
        self.navBar.addSubview(titleLab)
        
        self.resetBtn = UIButton(frame: CGRect(x: kScreenWidth - 50, y: NavBarHight - 44, width: 44, height: 44))
        self.resetBtn.backgroundColor = .white
        self.resetBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        self.resetBtn.setTitle(MMLocalizedString("reset", comment: "重置"), for: .normal)
        self.resetBtn.setTitleColor(Theme.mainColor, for: .normal)
        self.resetBtn.addTarget(self, action: #selector(resetBtnAntion), for: .touchUpInside)
        self.navBar.addSubview(self.resetBtn)
    }
    
    func initTalbeView(){
        self.tableView = BaseTableView.init(frame: CGRect.init(x: 0, y: NavBarHight, width: kScreenWidth, height: kScreenHeight - TabbarHeight - NavBarHight - 10), style: .grouped)
        self.tableView.register(ScreenTableViewCell.self, forCellReuseIdentifier: "ScreenTableViewCell")
        self.tableView.register(PriceRangeTableViewCell.self, forCellReuseIdentifier: "PriceRangeTableViewCell")//价格
        self.tableView.register(YearTableViewCell.self, forCellReuseIdentifier: "YearTableViewCell")
        self.tableView.register(SeatTableViewCell.self, forCellReuseIdentifier: "SeatTableViewCell")
        self.tableView.register(VehicleModelTableViewCell.self, forCellReuseIdentifier: "VehicleModelTableViewCell")
        self.tableView.register(CarSeriesTableViewCell.self, forCellReuseIdentifier: "CarSeriesTableViewCell")
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.backgroundColor = .white
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.estimatedRowHeight = 50
        self.tableView.separatorStyle = .none
        self.view.addSubview(self.tableView)
        
        
        let bgView = UIView(frame: CGRect(x: 0, y: kScreenHeight - TabbarHeight - 10, width: kScreenWidth, height: TabbarHeight))
        bgView.backgroundColor = .white
        self.view.addSubview(bgView)
        
        self.filtratBtn = UIButton(frame: CGRect(x: 14, y: 10, width: kScreenWidth - 28, height: 46))
        self.filtratBtn.backgroundColor = Theme.mainColor
        self.filtratBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
        self.filtratBtn.setTitle(MMLocalizedString("confirm", comment: "确定"), for: .normal)
        self.filtratBtn.setTitleColor(.white, for: .normal)
        self.filtratBtn.layer.cornerRadius = 8
        self.filtratBtn.clipsToBounds = true
        self.filtratBtn.addTarget(self, action: #selector(filtratBtnAntion), for: .touchUpInside)
        bgView.addSubview(self.filtratBtn)
        
    }
    
    func dealData(){
        
        if self.selectModel == nil{
            self.selectModel = SelectparameterModel.init()
        }
        
        //价格
        self.selectModel?.priceMin =  self.selectModel?.priceMin ?? MMLocalizedString("all", comment: "全部")
        self.selectModel?.priceMax = self.selectModel?.priceMax ?? MMLocalizedString("all", comment: "全部")
        
        
        //年
        self.selectModel.yearMin = self.selectModel?.yearMin ?? "1950"
        let currentYear = Calendar.current.component(.year, from: Date())
        self.selectModel.yearMax =  self.selectModel?.yearMax ?? "\(currentYear)"
   
        //品牌
        let brandModel = BrandSeriesListModel.init()
        brandModel.brandName = MMLocalizedString("all", comment: "全部")
        if self.selectModel.brandId ?? 0 > 0 {
            brandModel.isSelect = false
        }else{
            brandModel.isSelect = true
        }
        brandModel.id = -1
        self.brandNameArray.add(brandModel)
        
        //车系
        let seriesModel = SeriesListModel.init()
        seriesModel.name = MMLocalizedString("all", comment: "全部")
        if self.selectModel.seriesId ?? 0 > 0 {
            seriesModel.isSelect = false
        }else{
            seriesModel.isSelect = true
        }
        seriesModel.id = -1
        self.seriesArray.add(seriesModel)
        
        
        //座位
        let seats = [["name": MMLocalizedString("all", comment: "全部"),"value": -1],
                     ["name": MMLocalizedString("a_seat", comment: "1座"),"value": 1],
                     ["name": MMLocalizedString("two_seats", comment: "2座"),"value": 2],
                     ["name": MMLocalizedString("three_seats", comment: "3座"),"value": 3],
                     ["name": MMLocalizedString("four_seats", comment: "4座"),"value": 4],
                     ["name": MMLocalizedString("five_seats", comment: "5座"),"value": 5],
                     ["name": MMLocalizedString("six_seats", comment: "6座"),"value": 6],
                     ["name": MMLocalizedString("seven_seats", comment: "7座"),"value": 7]]
        
        for index in 0 ..< seats.count{
            let obj = seats[index]
            let seatModel = ScreenModel.init()
            seatModel.name = obj["name"]! as! String
            seatModel.value = obj["value"]! as! Int
            seatModel.isSelect = false
            if (self.selectModel.seatnumMax == seatModel.value) && self.isHomeEnter == 1{
                seatModel.isSelect = true
            }else if self.selectModel.seatnumMin ?? 0 > 0 {
                if seatModel.value == self.selectModel.seatnumMin ?? 0 {
                    seatModel.isSelect = true
                }
            }else if index == 0 && (self.selectModel.seatnumMax == nil){
                seatModel.isSelect = true
            }
            self.seatArray.add(seatModel)
        }
     
    }
    
    //车型列表
    func requestCarListData(){
        if self.seaCarFlag {
            //海外车
            //默认选中的品牌-车系
            self.getDefaultBrandData()
            
            //价格
            //self.selectModel.

            //处理车型
            let carTypes = self.originData?.bodyCDs ?? []
//            let tempArr = carTypes.map { str in
//                let item = CarModelDataList()
//                item.carCategory = str
//                item.isSelect = false
//                return item
//            }
            var tempArr: [CarModelDataList] = []
            for (pIndex, pObj) in carTypes.enumerated() {
                let item = CarModelDataList()
                item.carCategory = pObj
                item.isSelect = false
                item.id = pIndex + 1 //随机给大于0的值
                tempArr.append(item)
            }
            self.vehicleModelArray = tempArr
            
            //插入全部 车型
            let model = CarModelDataList.init()
            model.carCategory = MMLocalizedString("all", comment: "全部")
            model.isSelect = true
            model.id = -1
            self.vehicleModelArray.insert(model, at: 0)
            
            //默认选中车型
            if self.selectModel.carCategoryIds != "" {
                //有选中的车型
                self.vehicleModelArray[0].isSelect = false
                if let findObj = self.vehicleModelArray.first(where: {$0.carCategory == self.selectModel.carCategoryIds}) {
                    findObj.isSelect = true
                }
            }else{
                //默认全部
            }
            
            self.tableView.reloadData()
        }else{
            //本地车
            XTRequestCenter.shared.sendRequestMappableTargetObject(target: APIManager.carListData,objectType: CarModelList.self) { data in
                let resultObj = data as! CarModelList
                if resultObj.code == "1" {
                    self.vehicleModelArray = resultObj.data ?? []
                    let model = CarModelDataList.init()
                    model.carCategory = MMLocalizedString("all", comment: "全部")
                    model.isSelect = true
                    model.id = -1
                    self.vehicleModelArray.insert(model, at: 0)
                    if self.isHomeEnter == 0{
                        self.homePushDealData()
                    }else{
                        self.homeFiltratePushDealData()
                    }
                }
                self.tableView.reloadData()
            }error: { err in
            }
        }
        
    }
    
    ///海外车
    ///获取默认选中的品牌
    private func getDefaultBrandData(){
        
        let tempList = self.originData?.katashikiList ?? []
        tempList.forEach { item in
            if item.first == nil || item.first == "" {
                item.first = "#"
            }
        }
        
        let resultArr = tempList.map { katashikiListData in
            let item = BrandSeriesListModel()
            item.brandName = katashikiListData.brandName ?? ""
            item.id = Int(katashikiListData.brandId ?? "0")
            item.isSelect = false
            item.first = katashikiListData.first ?? "#"
            let list = katashikiListData.katashiki ?? []
            
            var tempList:[SeriesListModel] = []
            for (pIndex, pObj) in list.enumerated() {
                let pItem = SeriesListModel()
                pItem.isSelect = false
                pItem.name = pObj
                pItem.id = pIndex //注：海外车车系是没有id的
                tempList.append(pItem)
            }
            item.seriesList = tempList
            return item
        }
        
        if self.selectModel.brandId ?? 0 > 0 {
            //有默认选中的
            //找到选中的品牌
            if let firObj = resultArr.first(where: {$0.id ?? 0 == self.selectModel.brandId ?? 0}) {
                firObj.isSelect = true
                self.brandNameArray.add(firObj)
                //找到选中的品牌对应的车系
                let tempSer = firObj.seriesList ?? []
                tempSer.forEach { item in
                    if item.name ?? "" == self.selectModel.seriesName {
                        item.isSelect = true
                    }
                }
                self.seriesArray.addObjects(from: tempSer)
            }
        }else{
//            if self.seatArray
        }
    }
    
    //首页车型进来
    func homePushDealData(){
       _ = self.vehicleModelArray.map { item in
            if self.selectModel.carCategoryIds == "\(item.id ?? 0 )"{
                self.vehicleModelArray = []
                item.isSelect = true
                self.vehicleModelArray.append(item)
            }
        }
    }
    
    
    //首页筛选进来的要记住之前选中的
    func homeFiltratePushDealData(){
        if self.selectModel.carCategoryIds == ""{
            return
        }
        _ = self.vehicleModelArray.map { item in
            if self.selectModel.carCategoryIds.contains("\(item.id ?? 0 )"){
                item.isSelect = true
            }else{
                item.isSelect = false
            }
        }
    }

    //MARK: - Antion
    @objc func closeBtnAntion(){
        if self.navigationController?.viewControllers.count ?? 0 > 1 {
            self.navigationController?.popViewController(animated: true)
        }else{
            self.dismiss(animated: true)
        }
    }
    
    //重置
    @objc func resetBtnAntion() {
        //品牌
        if let arr = self.brandNameArray as? [BrandSeriesListModel] {
            arr.forEach{$0.isSelect = false}
            arr.first?.isSelect = true
            self.selectModel.brandId = -1
        }
        
        //车系-品牌重置后，也默认全选
        if let arr = self.seriesArray as? [SeriesListModel] {
            arr.forEach{$0.isSelect = false}
            arr.first?.isSelect = true
            self.selectModel.seriesName = ""
        }

        //价格
//        self.selectModel?.priceMaxShow = MMLocalizedString("all", comment: "全部")
//        self.selectModel?.priceMinShow = MMLocalizedString("all", comment: "全部")
        self.selectModel?.priceMax = MMLocalizedString("all", comment: "全部")
        self.selectModel?.priceMin = MMLocalizedString("all", comment: "全部")
        self.selectModel?.sellingPriceTypeCode = ""
        
        //座位
        for index in 0 ..< self.seatArray.count{
            let model = self.seatArray[index] as! ScreenModel
            if model.value == -1{
                model.isSelect = true
            }else{
                model.isSelect = false
            }
        }
        
        
        self.selectModel.yearMin =  "1950"
        let currentYear = Calendar.current.component(.year, from: Date())
        self.selectModel.yearMax =  "\(currentYear)"

        //车型
        if self.vehicleModelArray.count > 0 {
            self.vehicleModelArray.forEach{$0.isSelect = false}
            self.vehicleModelArray[0].isSelect = true
        }
        self.selectModel.bodyCD = ""
        self.selectModel.carCategoryIds = ""
        self.tableView.reloadData()
    }
    
    @objc func filtratBtnAntion(){
        
        //品牌
        self.selectModel?.brandId = 0
        if let arr = self.brandNameArray as? [BrandSeriesListModel] {
            if let findObj = arr.first(where: {$0.isSelect}) {
                self.selectModel.brandId = findObj.id ?? 0
            }
        }
       
        
        //车系
        if let arr = self.seriesArray as? [SeriesListModel] {
            if let findObj = arr.first(where: {$0.isSelect}) {
                self.selectModel.seriesName = findObj.name ?? ""
                self.selectModel.seriesId = findObj.id ?? 0
            }
        }
        
        //车型
        let selectArray = NSMutableArray()
        for index in 0 ..< self.vehicleModelArray.count{
            let model = self.vehicleModelArray[index]
            if model.isSelect == true{
                if self.seaCarFlag && model.id ?? 0 > 0 {
                    //海外车
                    selectArray.add(model.carCategory ?? "")
                }else{
                    selectArray.add("\(model.id ?? 0)")
                }
            }
        }

        let carModelId = selectArray.componentsJoined(by: ",")
        if carModelId.contains("-1"){
            //如果是全部不用赋值
            self.selectModel?.carCategoryIds = ""
        }else{
            self.selectModel?.carCategoryIds = carModelId
        }
        
        //座位
        for index in 0 ..< self.seatArray.count{
            let model = self.seatArray[index] as! ScreenModel
            if model.isSelect == true{
                self.selectModel?.seatnumMax = model.value
                self.selectModel?.seatnumMin = model.value
            }
        }
        
        if self.isHomeEnter == 1{//首页进来
            let vc = UsedCarViewController()
            vc.selectParam = self.selectModel
            vc.isHomeEnter = self.isHomeEnter
            self.navigationController?.pushViewController(vc, animated: true)
        }else{
            self.screeningConditionBlock?(self.selectModel)
            self.closeBtnAntion()
        }
    }
}

extension ScreenViewController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.sectionsArray.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if self.seaCarFlag && indexPath.section == 3 {
            //海外车不显示车系
            return 0.01
        }
        return UITableViewAutomaticDimension
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "ScreenTableViewCell", for: indexPath) as! ScreenTableViewCell
            if self.brandNameArray.count > 0{
                cell.setData(data: self.brandNameArray)
            }
            return cell
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: "CarSeriesTableViewCell", for: indexPath) as! CarSeriesTableViewCell
                cell.setData(data: self.seriesArray)
            return cell
        case 2://价格区间
            let cell = tableView.dequeueReusableCell(withIdentifier: "PriceRangeTableViewCell", for: indexPath) as!
            PriceRangeTableViewCell
            if self.selectModel != nil{
                cell.startPriceBtn.setTitle(self.selectModel?.priceMin, for: .normal)
                cell.endPriceBtn.setTitle(self.selectModel?.priceMax, for: .normal)
            }
            cell.delegate = self
            return cell
        case 3://座位
            let cell = tableView.dequeueReusableCell(withIdentifier: "SeatTableViewCell", for: indexPath) as! SeatTableViewCell
            if self.seatArray.count > 0{
                cell.setData(data: self.seatArray as! [ScreenModel])
            }
            return cell
        case 4:// 制造年份
            let cell = tableView.dequeueReusableCell(withIdentifier: "YearTableViewCell", for: indexPath) as!
            YearTableViewCell
            if self.selectModel != nil{
                cell.startYearBtn.setTitle(self.selectModel?.yearMin, for: .normal)
                cell.endYearBtn.setTitle(self.selectModel?.yearMax, for: .normal)
            }
            cell.delegate = self
            return cell
        default://车型
            let cell = tableView.dequeueReusableCell(withIdentifier: "VehicleModelTableViewCell", for: indexPath) as!     VehicleModelTableViewCell
                cell.signleFlag = self.originData != nil
            if self.vehicleModelArray.count > 0 {
                cell.setData(data: self.vehicleModelArray)
            }
            return cell
        }
        
    }
    
    
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let bgView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 0.01))
        bgView.backgroundColor = .white
        return bgView
    }
    
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if self.seaCarFlag && section == 3 {
            return 0.01
        }
        return 40
    }
    
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let bgView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 40))
        bgView.clipsToBounds = true
        bgView.backgroundColor = .white
        
        let titleLab = UILabel(frame: CGRect(x: 20, y: 0, width: kScreenWidth - 130, height: bgView.height))
        titleLab.textColor = Theme.titleLabColor
        titleLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 15)
        titleLab.isHidden = false
        titleLab.textAlignment = .left
        bgView.addSubview(titleLab)
        titleLab.text = self.sectionsArray[section]
        
        if section == 0 {
            let allBrandsBtn = UIButton(frame: .zero)
            allBrandsBtn.setTitle(MMLocalizedString("all_brands", comment: "全部品牌"), for: .normal)
            allBrandsBtn.setTitleColor(Theme.titleLabColor, for: .normal)
            allBrandsBtn.titleLabel?.font = UIFont(name: Theme.PingFangSCRegular, size: 12)
            allBrandsBtn.addTarget(self, action: #selector(allBrandsBtnAntion), for: .touchUpInside)
            allBrandsBtn.setImage(UIImage(named: "icon_news_arrows"), for: .normal)
            allBrandsBtn.semanticContentAttribute = .forceRightToLeft
            bgView.addSubview(allBrandsBtn)
            
            allBrandsBtn.snp.makeConstraints { make in
                make.top.equalTo(0)
                make.right.equalTo(-14)
                make.height.equalTo(40)
            }
        }
        return bgView
    }
    
    
    //全部品牌
    @objc func allBrandsBtnAntion() {
        let vc = AllBrandsViewController()
        vc.delegate = self
        vc.seaData = self.originData
        vc.selectBrandArray = self.brandNameArray
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}

//品牌选择
extension ScreenViewController: BrandSearchViewDelegate{
    func selectBrandData(dataArray: NSMutableArray) {
        self.seriesArray.removeAllObjects()
        if dataArray.count == 0{
            return
        }
        //车系
        let getBrandModel = dataArray[0] as! BrandSeriesListModel
        self.seriesArray.addObjects(from: getBrandModel.seriesList ?? [])

        let seriesModel = SeriesListModel.init()
        seriesModel.name = MMLocalizedString("all", comment: "全部")
        seriesModel.id = -1
        seriesModel.isSelect = true
        self.seriesArray.insert(seriesModel, at: 0)
        
        //品牌
        let brandModel = BrandSeriesListModel.init()
        brandModel.brandName = MMLocalizedString("all", comment: "全部")
        brandModel.isSelect = false
        brandModel.id = -1
        dataArray.insert(brandModel, at: 0)
        
        self.brandNameArray = dataArray
        self.tableView.reloadData()
    }
}

extension ScreenViewController: PriceRangeTableViewCellDelegate{
    func selectPriceRange() {
        let pickerView = CustomPickerView.init(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
        if self.seaCarFlag {
            //海外车
            let priceTypes = self.originData?.sellingPriceType ?? []
            var tempList = priceTypes.map { item in
                return item.desc ?? ""
            }
            tempList.insert(MMLocalizedString("all", comment: "全部"), at: 0)
            pickerView.dataArray = tempList
        }else{
            //本地车
            pickerView.dataArray = [
                MMLocalizedString("all", comment: "全部"),
                "0 - HK$50,000",
                "HK$50,000 - HK$100,000",
                "HK$100,000 - HK$150,000",
                "HK$150,000 - HK$200,000",
                "HK$200,000 - HK$300,000",
                "HK$300,000 - HK$500,000",
                "HK$500,000 - \(MMLocalizedString("above", comment: "以上"))"]
        }
        pickerView.selecScroll(row: self.selectModel?.index ?? 0)
        pickerView.show()
        weak var weakSelf = self
        pickerView.selectContentBlock = { conten, row in
            weakSelf?.selectModel.priceGroup = conten
            weakSelf?.selectModel.index = row
            switch row {
            case 0:
                weakSelf?.selectModel?.priceMin = MMLocalizedString("all", comment: "全部")
                weakSelf?.selectModel?.priceMax = MMLocalizedString("all", comment: "全部")
                default:
                    if row == pickerView.dataArray.count - 1 {
                        //最后一个，只取最小价格
//                        weakSelf?.selectModel?.priceMinShow = conten
//                        weakSelf?.selectModel?.priceMaxShow = conten
                        let firstStr = conten.components(separatedBy: "-").first
                        //let lastStr = conten.components(separatedBy: "-").last
                        let priceMin = (firstStr?.components(separatedBy: "HK$").last ?? "").removeAllSapce
//                        let priceMan = (lastStr?.components(separatedBy: "HK$").last ?? "").removeAllSapce
                        weakSelf?.selectModel?.priceMin = priceMin.replacingOccurrences(of: ",", with: "")
                        weakSelf?.selectModel?.priceMax = priceMin.replacingOccurrences(of: ",", with: "")
                    }else{
                        let firstStr = conten.components(separatedBy: "-").first
                        let lastStr = conten.components(separatedBy: "-").last
                        let priceMin = (firstStr?.components(separatedBy: "HK$").last ?? "").removeAllSapce
                        let priceMan = (lastStr?.components(separatedBy: "HK$").last ?? "").removeAllSapce
//                        weakSelf?.selectModel?.priceMinShow = firstStr
//                        weakSelf?.selectModel?.priceMaxShow = lastStr
                        weakSelf?.selectModel?.priceMin = priceMin.replacingOccurrences(of: ",", with: "")
                        weakSelf?.selectModel?.priceMax = priceMan.replacingOccurrences(of: ",", with: "")
                        
                        if self.seaCarFlag {
                            var tempArr = weakSelf?.originData?.sellingPriceType ?? []
                            tempArr.insert(SellingPriceTypeData(), at: 0) //因为第一个是全部
                            if tempArr.count > row {
                                weakSelf?.selectModel.sellingPriceTypeCode = "\(tempArr[row].code ?? 0)"
                            }
                        }
                        
                    }
            }
            weakSelf?.tableView.reloadData()
        }
    }
}

extension ScreenViewController: YearTableViewCellDelegate{
    func selectDateCell(tag: NSInteger) {
        let datePicker1 = YearDatePickerView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
        if  self.seaCarFlag  {
            datePicker1.titleStr = MMLocalizedString("year_manufacture", comment: "年份")
        }else{
            datePicker1.titleStr = MMLocalizedString("year_manufacture_local", comment: "制造年份")
        } 
//        let currentYear = Calendar.current.component(.year, from: Date())
        datePicker1.scrollTo(self.selectModel?.yearMin, end: self.selectModel?.yearMax ?? "")
        datePicker1.show()
        datePicker1.selectYearBlock = { [weak self] yearMin, yearMax in
            self?.selectModel?.yearMin = yearMin
            self?.selectModel?.yearMax = yearMax
            self?.tableView.reloadData()
        }
        
    }
}



